package gdut.jmxywz.action;

import gdut.jmxywz.been.Bookmaking;
import gdut.jmxywz.been.Financial;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import jxl.format.Alignment;
import jxl.write.WritableCellFormat;
import jxl.write.WriteException;

import com.gzbugu.excel.ExcelDesign;
import com.gzbugu.excel.GzbuguExportExcelUtil;

import org.apache.struts2.ServletActionContext;
import org.apache.velocity.runtime.parser.ParseException;

import com.opensymphony.xwork2.ActionContext;

/**
 * @说明 此方法用于分页查询
 * @param 需要参数objectName，查询条件名称value，查询条件condiction，排序方式ascorDesc
 * 当前页数currentPage，每页记录数pageSize
 * @author 宋樟潮
 * */
public class SalaryDetailAction extends BaseAction {
	
	private Teacher teacher;
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	private Financial financial = new Financial();
	private String finanId;
	private String finanName;
	private Long amount;
	private String department;
	private Timestamp extendTime;
	private String month;
	private String semester;
	private String syear;
	private Long setMoney;
	
	private PageModel pm = new PageModel();
	private int currentPage;
	private int pageSize;
	private String nextPage;
	
	private String teacherName;
	
	/**excel导入导出*/
	private File excelFile;
	private String excelFileContentType;
	private boolean checkbox;
	
	
	/**
	 * 薪酬列表-查询
	 * @return
	 * @throws Exception
	 */
	public String salaryDetail() throws Exception{
		if(currentPage<=0){
			currentPage=1;
		}
		if(pageSize<=0){
			pageSize=15;
		}
		teacher = (Teacher) ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		if(teacher!=null&&!"".equals(teacher)){
			if(teacher.getTeacherId()==47){
				String hql = "from Financial where 1=1 order by extendTime desc";
				pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, "salaryDetail.action", "");

			}else{
		String tName = teacher.getTeacherName();
	    	String  hql = "from Financial financial where teacherName='"+tName+"' " ;
		if((!"".equals(syear))&&syear!=null){
			hql = hql+" and syear like '%"+syear+"%'";
		}
		if(month!=null&&(!"".equals(month))){
			hql = hql+" and month like '%"+month+"%'";
		}
		if(semester!=null&&(!"".equals(semester))){
			hql = hql+" and semester like '%"+semester+"%'";
		}
		
		hql = hql+ " order by extendTime desc";
		
		pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, "salaryDetail.action", "");

			}
		return SUCCESS;
			
		}else{
			return "error";
		}
	}
	
	
	
	
	/**
	 * @说明 管理员录入薪酬明细
	 * @author 宋樟潮
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addSalaryDetail() throws Exception{
		try{	
		teacher = (Teacher) ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		financial.setTeacherByEditor(teacher);
		
		financial.setTeacherName(teacherName);
		//long salary = Integer.parseInt(amount); 
		 financial.setFinanName(finanName);
		 financial.setAmount(amount);
		//financial.setTotal(null);
		//financial.setSetMoney(null);
		financial.setExtendTime(extendTime);
	   	financial.setDepartment(department);
		}catch(Exception e){
			e.printStackTrace();
		}
		if(!"".equals(syear)&&null!=syear){
			System.out.println(finanName);
		    financial.setSyear(syear);
		}
		if(!"".equals(semester)&&null!=semester){
		 	financial.setSemester(semester);
			}
		if(!"".equals(month)&&null!=month){
		 	financial.setMonth(month);
			}
		
		boolean bool = this.getCommonService().saveObject(financial);
		if(bool){
		return SUCCESS;
		}else{
			return "error";
		}
	}
	
	
	/**
	 * @说明 批量导入薪酬
	 * @return
	 * @throws Exception
	 */
	public String financialImport() throws Exception{
		
		  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		  if(excelFile==null){
			  
			  return "error";
		  }
		  
		  int count=0;
			try{
			FileInputStream fis=new FileInputStream(excelFile);
			jxl.Workbook jwb=Workbook.getWorkbook(fis);
			int sheets=jwb.getNumberOfSheets();
			for(int i=0;i<sheets;i++){
				Sheet rs=jwb.getSheet(i);
				int columnNum=rs.getColumns();
				int rowNum=rs.getRows();
				if(rowNum>0&&columnNum!=8){
					System.out.println("--------------------导入格式不对----------------------");
					return "error";
				}
				Cell crl;
				boolean bool;
				for(int row=1;row<rowNum;row++){
					financial = new Financial();
					
					crl=rs.getCell(0, row);
					financial.setFinanName(crl.getContents());
					
					crl=rs.getCell(1, row);
					financial.setTeacherName(crl.getContents());
					
					crl=rs.getCell(2,row);
					financial.setAmount((long)Integer.parseInt(crl.getContents()));
	                 
					crl=rs.getCell(3, row);
					financial.setDepartment(crl.getContents());
					
					crl=rs.getCell(4, row);
					SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
					//sdf.setLenient(true);
					try{
						Date date = sdf.parse(crl.getContents());
						//String time = sdf.format(date);
						Timestamp tempTime = new Timestamp(date.getTime());
						financial.setExtendTime(tempTime);
					}catch(Exception e){
						e.printStackTrace();
					}
					
					crl=rs.getCell(5, row);
					financial.setMonth(crl.getContents());
					
					crl=rs.getCell(6, row);
					financial.setSemester(crl.getContents());
					
					crl=rs.getCell(7, row);
					financial.setSyear(crl.getContents());
					
					bool =this.getCommonService().saveObject(financial);
					if(!bool){
						System.out.println("------------------存入数据库出错-------------------");
					}
					
					count++;
					
				}
				
			}
	                 System.out.println("=================一共导入数据数：================="+count);
	     			jwb.close();
	     			fis.close();
	     			}catch(Exception e){
	     				e.printStackTrace();
	     			}
		return SUCCESS;
	}
	
	
	
	/**
	 * @说明 根据权限批量导出薪酬
	 * @return
	 * @throws WriteException
	*/
	@SuppressWarnings("unchecked")
	public void exportAllExcel() throws WriteException{
		
		  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		  teacherName = teacher.getTeacherName();
		  	
 try{
	   String[] str ={"项目名称","发放对象","项目金额","发放部门","发放时间","月份","学期","学年"};
	   List<Financial> list = new ArrayList<Financial>();
	   String hql = "from Financial where 1=1 ";
	   list =this.getCommonService().findListByHql(hql, null);
	   
	   HttpServletResponse response = ServletActionContext.getResponse();
	   response.setCharacterEncoding("UTF-8");
	   response.reset();
	   response.setContentType("application/vnd.ms-excel");
	   response.setContentType("APPLICATION/OCTET-STREAM");
	   response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("薪酬明细.xls","UTF-8"));
	   WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream());
	   WritableSheet sheet = wwb.createSheet("sheet1", 0);
	   
	    sheet.setColumnView(0, 10);
		sheet.setColumnView(1, 10);
		sheet.setColumnView(2, 15);
		sheet.setColumnView(3, 10);
			 Label label = null;
			 for(int i=0;i<str.length;i++){
				 label = new Label(i,0,str[i]);
				 sheet.addCell(label);
			 }
			 for(int i=0;i<list.size();i++){
				financial = list.get(i);
				 
				label = new Label(0,i+1,financial.getFinanName());
				sheet.addCell(label);
				label = new Label(1,i+1,financial.getTeacherName());
				sheet.addCell(label);
				label = new Label(2,i+1,""+financial.getAmount());
				//label = new Label(3,i+1,String.valueOf(financial.getAmount()));
				sheet.addCell(label);
				label = new Label(3,i+1,financial.getDepartment());
				sheet.addCell(label);
				if(financial.getExtendTime()!=null){
				label = new Label(4,i+1,""+financial.getExtendTime());
				sheet.addCell(label);
				label = new Label(5,i+1,financial.getMonth());
				sheet.addCell(label);
				label = new Label(6,i+1,financial.getSemester());
				sheet.addCell(label);
				label = new Label(7,i+1,financial.getSyear());
				sheet.addCell(label);
				}
			 }
		
		wwb.write();
		wwb.close();
			 
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		  
	}

	
	/**
	 * @说明 非管理员批量导出薪酬
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void exportExcel() throws Exception{
		
		
			
		  try{
			   String[] str ={"项目名称","发放对象","项目金额","发放部门","发放时间","月份","学期","学年"};
			   List<Financial> list = new ArrayList<Financial>();
			   String hql = "from Financial where 1=1 ";
			   list =this.getCommonService().findListByHql(hql, null);
			   
			   HttpServletResponse response = ServletActionContext.getResponse();
			   response.setCharacterEncoding("UTF-8");
			   response.reset();
			   response.setContentType("application/vnd.ms-excel");
			   response.setContentType("APPLICATION/OCTET-STREAM");
			   response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("薪酬明细.xls","UTF-8"));
			   WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream());
			   WritableSheet sheet = wwb.createSheet("sheet1", 0);
			   
			    sheet.setColumnView(0, 10);
				sheet.setColumnView(1, 10);
				sheet.setColumnView(2, 15);
				sheet.setColumnView(3, 10);
					 Label label = null;
					 for(int i=0;i<str.length;i++){
						 label = new Label(i,0,str[i]);
						 sheet.addCell(label);
					 }
					 for(int i=0;i<list.size();i++){
						financial = list.get(i);
						 
						label = new Label(0,i+1,financial.getFinanName());
						sheet.addCell(label);
						label = new Label(1,i+1,financial.getTeacherName());
						sheet.addCell(label);
						label = new Label(2,i+1,""+financial.getAmount());
						//label = new Label(3,i+1,String.valueOf(financial.getAmount()));
						sheet.addCell(label);
						label = new Label(3,i+1,financial.getDepartment());
						sheet.addCell(label);
						if(financial.getExtendTime()!=null){
						label = new Label(4,i+1,""+financial.getExtendTime());
						sheet.addCell(label);
						label = new Label(5,i+1,financial.getMonth());
						sheet.addCell(label);
						label = new Label(6,i+1,financial.getSemester());
						sheet.addCell(label);
						label = new Label(7,i+1,financial.getSyear());
						sheet.addCell(label);
						}
					 }
				
				wwb.write();
				wwb.close();
					 
				   }catch(Exception e){
					   e.printStackTrace();
				   }
	}
	
	
	
	
	
	
	/*****************setter and getter***********************/
	
	public Financial getFinancial() {
		return financial;
	}
	public void setFinancial(Financial financial) {
		this.financial = financial;
	}
	public String getFinanId() {
		return finanId;
	}
	public void setFinanId(String finanId) {
		this.finanId = finanId;
	}
	public String getFinanName() {
		return finanName;
	}
	public void setFinanName(String finanName) {
		this.finanName = finanName;
	}
	public Long getAmount() {
		return amount;
	}
	public void setAmount(Long amount) {
		this.amount = amount;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public Timestamp getExtendTime() {
		return extendTime;
	}
	public void setExtendTime(Timestamp extendTime) {
		this.extendTime = extendTime;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getSemester() {
		return semester;
	}
	public void setSemester(String semester) {
		this.semester = semester;
	}
	public String getSyear() {
		return syear;
	}
	public void setSyear(String syear) {
		this.syear = syear;
	}
	public Long getSetMoney() {
		return setMoney;
	}
	public void setSetMoney(Long setMoney) {
		this.setMoney = setMoney;
	}
	public PageModel getPm() {
		return pm;
	}
	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}



	public String getNextPage() {
		return nextPage;
	}


	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}




	public Teacher getTeacher() {
		return teacher;
	}


	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}


	public String getTeacherName() {
		return teacherName;
	}


	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}




	public File getExcelFile() {
		return excelFile;
	}




	public void setExcelFile(File excelFile) {
		this.excelFile = excelFile;
	}




	public String getExcelFileContentType() {
		return excelFileContentType;
	}




	public void setExcelFileContentType(String excelFileContentType) {
		this.excelFileContentType = excelFileContentType;
	}




	public boolean isCheckbox() {
		return checkbox;
	}




	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

}
